<template>
  <a-modal
    width="35%"
    title="生成注册码"
    :dialog-style="{ top: '20px' }"
    :visible="openDialog"
    :maskClosable="false"
    :keyboard="false"
    :footer="null"
    @cancel="mainDialogClose()"
  >
    <!-- @cancel="mainDialogClose('commonForm')" -->

    <a-form-model :model="form" :label-col="labelCol" :wrapper-col="wrapperCol">
      <a-form-model-item label="SN码">
        <span>{{ params.psnCode || "" }}</span>
      </a-form-model-item>

      <a-form-model-item label="清除下级数据">
        <a-radio-group v-model="value" @change="onChange">
          <a-radio :value="1">
            <span>是</span>
          </a-radio>
          <a-radio :value="0">
            <span>否</span>
          </a-radio>
        </a-radio-group>
      </a-form-model-item>
      <a-form-model-item label="注册时间">
        <a-radio-group v-model="form.forever" @change="foreverChange">
          <a-radio :value="1">
            <span>永久</span>
          </a-radio>
          <a-radio :value="0">
            <span>自定义</span>
          </a-radio>
        </a-radio-group>
      </a-form-model-item>

      <!-- <a-form-model-item label="注册时间" v-if="form.forever == 0">
          <a-select  :getPopupContainer="(triggerNode) => triggerNode.parentNode" v-model="form.type" placeholder="请选择注册天数" style="width: 40%;" @change="DaysChange">
            <a-select-option value="1">
              一天
            </a-select-option>
            <a-select-option value="2">
              一周
            </a-select-option>
            <a-select-option value="3">
              一月
            </a-select-option>
            <a-select-option value="4">
              一年
            </a-select-option>
            <a-select-option value="0">
              自定义
            </a-select-option>
          </a-select>
          <span style="margin-left: 16px;">{{registerTime}}</span>
        </a-form-model-item>  -->
      <a-form-model-item label="到期时间" v-if="form.forever == 0">
        <a-date-picker
          size="small"
          @change="datechange"
          :disabledDate="disabledDate"
        />
        <div>
          <span class="end_time">授权截止到:</span>
          {{ times.year }} 年 {{ times.month }} 月 {{ times.day }} 日 12:00时
        </div>
      </a-form-model-item>

      <a-form-model-item label="注册码" v-if="RegistCode != ''">
        <span>{{ RegistCode || "" }}</span>
      </a-form-model-item>

      <a-form-model-item :wrapper-col="{ span: 14, offset: 10 }">
        <a-button type="primary" @click="getRegistCode">获取注册码</a-button>
      </a-form-model-item>
    </a-form-model>
  </a-modal>
</template>

<script>
import api from "@/api/apiServer";

export default {
  name: "detailPartDialog",
  props: {
    openDialog: Boolean,
    title: String,
    params: Object
  },
  components: {},
  data() {
    return {
      // username: '',
      // role: '',
      // compId: '',
      // compType: '',
      fileList: [],
      upLoadFile: "",
      RegistCode: "",
      labelCol: { span: 4 },
      wrapperCol: { span: 14 },
      form: {
        type: undefined,
        forever: 0
      },
      value: 0,
      clrFlag: "",
      times: {
        year: "--",
        month: "--",
        day: "--"
      },
      timeParams: {
        macSN: "",
        day: ""
      },
      clrFlag: 0
    };
  },
  watch: {
    openDialog: {
      immediate: false,
      handler: async function(newVal, oldVal) {
        this.fileList = [];
      }
    }
  },
  methods: {
    datechange(data, datastring) {
      console.log("时间", datastring.split("-"));
      this.times.year = datastring.split("-")[0];
      this.times.month = datastring.split("-")[1];
      this.times.day = datastring.split("-")[2];
      this.timeParams.day = datastring;
      // this.getProduceByMacSN();
      // this.getStateTimeSum();
      // this.DefMonthProduceDay();

      //生成注册码时修改时间，之前生成的码失效
      this.RegistCode = "";
    },

    mainDialogClose(formName) {
      this.$emit("update:openDialog", false);
      // this.$emit('closeDialogRefreshData', '');
    },
    //修改注册状态是否永久
    foreverChange(e) {
      console.log("输出", e.target.value);
      if (e.target.value == 1) {
        this.RegistCode = "";
        this.timeParams.day = `${this.$Moment()
          .add(100, "years")
          .format("YYYY-MM-DD")}`;
      } else {
        this.timeParams.day = "";
      }
    },
    onChange(e) {
      this.clrFlag = e.target.value;
    },
    //获取注册码
    getRegistCode() {
      console.log(this.timeParams.day);
      let pars = this.params;
      let params = {
        endUseDate: this.timeParams.day,
        dieDate: this.timeParams.day,
        clrFlag: this.clrFlag,
        registDate: `${this.$Moment().format("YYYY-MM-DD HH:mm:ss")}`,
        ...pars
      };
      if (this.timeParams.day != "" || this.form.forever == 1) {
        api.getRegistCode(params).then(res => {
          if (res.MsgCode == 0) {
            this.$message.success(res.MsgDesc);
          } else {
            this.$message.error(res.MsgDesc);
          }
          this.RegistCode = res.Data;
        });
      } else {
        this.$message.error("请选择时间");
      }
    },
    //日期选择不能选今天及之前的
    disabledDate(current) {
      return current && current < this.$Moment().endOf("day");
    }
  },
  mounted() {},
  created() {}
};
</script>

<style scoped>
.uploadText {
  width: 100%;
  text-align: center;
  padding: 4px;
  border: 1px solid #ebedf0;
}

.ant-upload.ant-upload-select.ant-upload-select-text {
  width: 100%;
}

.uploadBox {
  width: 100%;
  height: 80px;
  background-color: #fafafa;
  border: 1px dashed #d9d9d9;
  border-radius: 4px;
  cursor: pointer;
  text-align: left;
  font-size: 18px;
  color: #9d9d9d;
  line-height: 36px;
}
.end_time {
  margin-right: 20px;
}
</style>
